package jc.malpractice.controllers;

import cn.hutool.json.JSONObject;
import jc.malpractice.service.DeptService;
import jc.malpractice.service.EventService;
import jc.malpractice.service.UserService;
import spark.Route;

import java.awt.*;

import static spark.Spark.get;
import static spark.Spark.post;

public class EventController {

    static  Route upload_event =(req,res)->{
        //

        JSONObject handler = UserService.selectHanderAll(req.queryParams("handler_name"));
        JSONObject handler_all = handler.getJSONArray("data").getJSONObject(0);
        System.out.println("handler:"+handler_all);






        //
        String user_code = req.raw().getParameter("user_code");
        String user_name = req.raw().getParameter("user_name");
        String dept_name = req.raw().getParameter("dept_name");
        String dept_code = req.raw().getParameter("dept_code");
        String handler_name = req.raw().getParameter("handler_name");
        String status =req.raw().getParameter("status");


        String title = req.raw().getParameter("title");
        String content = req.raw().getParameter("content");
        String cause  = req.raw().getParameter("cuase");
        String date = req.raw().getParameter("date");
        String injury_level = req.raw().getParameter("injury_level");
        String memo = req.raw().getParameter("memo");
        String time_elapsed = req.raw().getParameter("time_elapsed");
//        System.out.println(dept);
        System.out.println(handler_name);
        System.out.println(date);

        //handler_all.getStr("reporter_name"), ,
        return EventService.upload_event(handler_name,title,content,cause,date,injury_level,memo,time_elapsed
                ,user_code,user_name,dept_code,dept_name,status,
                handler_all.getStr("reporter_code"),handler_all.getStr("dept_code"),handler_all.getStr("dept_name"));
    };

    static Route selectHandleEvent = (req, res) -> {
        JSONObject jo = EventService.selectHandleEvent(req.queryParams("handler_id"));

        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "session失效");
        }

        //req.session().attribute("username", jo.get("password"));
        return jo;
    };

    static Route selectReportEvent = (req, res) -> {
        JSONObject jo = EventService.selectReportEvent(req.queryParams("reporter_id"));

        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "session失效");
        }

        //req.session().attribute("username", jo.get("password"));
        return jo;
    };

    static Route accept_event = (req, res) -> {
        JSONObject jo = EventService.accept_event(req.queryParams("handler_id"));

        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "session失效");
        }

        //req.session().attribute("username", jo.get("password"));
        return jo;
    };

    static Route handle_event = (req, res) -> {
        JSONObject jo = EventService.handle_event(req.queryParams("handler_id"),req.queryParams("result"));

        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "session失效");
        }

        //req.session().attribute("username", jo.get("password"));
        return jo;
    };

    static Route selectEventById = (req, res) -> {
        JSONObject jo = EventService.selectEventById(req.queryParams("id"));

        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "数据获取失败");
        }

        return jo;
    };

    static Route event_status = (req, res) -> {
        JSONObject jo = EventService.event_status(req.queryParams("id"));

        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "数据获取失败");
        }

        return jo;
    };

    static Route updateEvent1 = (req, res) -> {
        JSONObject handler = UserService.selectHanderAll(req.queryParams("handler"));
        JSONObject handler_all = handler.getJSONArray("data").getJSONObject(0);
        JSONObject jo = EventService.updateEvent1(req.queryParams("reporter"),req.queryParams("title"),req.queryParams("content"),
                req.queryParams("date"),req.queryParams("time_elapsed"),handler_all.getStr("reporter_code"),
                handler_all.getStr("reporter_name"), handler_all.getStr("dept_code"),handler_all.getStr("dept_name"),
                req.queryParams("cause"),req.queryParams("injury_level"),req.queryParams("memo"));
        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "数据修改失败");
        }

        System.out.println("handler:"+handler_all);

        return jo;
    };

    static Route updateEvent2 = (req, res) -> {
        JSONObject handler = UserService.selectHanderAll(req.queryParams("handler"));
        JSONObject handler_all = handler.getJSONArray("data").getJSONObject(0);
        JSONObject jo = EventService.updateEvent2(req.queryParams("reporter"),req.queryParams("title"),req.queryParams("content"),
                req.queryParams("date"),req.queryParams("time_elapsed"),handler_all.getStr("reporter_code"),
                handler_all.getStr("reporter_name"), handler_all.getStr("dept_code"),handler_all.getStr("dept_name"),
                req.queryParams("cause"),req.queryParams("injury_level"),req.queryParams("memo"));
        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "数据修改失败");
        }

        System.out.println("handler:"+handler_all);

        return jo;
    };

    public static void Init() {
        post("/test/upload_event",upload_event);
        get("/user/selectHandleEvent", selectHandleEvent);
        get("/user/selectReportEvent", selectReportEvent);
        get("/user/event_status", event_status);
        get("/user/accept_event", accept_event);
        get("/user/handle_event", handle_event);
        post("/user/selectEventById", selectEventById);
        post("/user/updateEvent1", updateEvent1);
        post("/user/updateEvent2", updateEvent2);
    }
}
